package io.adrainty.bolee.security.repository.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.adrainty.bolee.framework.commons.utils.IdentifierNoUtil;
import io.adrainty.bolee.security.mapper.IResourceMapper;
import io.adrainty.bolee.security.models.bo.ResourceBO;
import io.adrainty.bolee.security.models.constants.ResourceConstants;
import io.adrainty.bolee.security.models.entity.ResourceEntity;
import io.adrainty.bolee.security.repository.IResourceRepository;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author AdRainty
 * @version V1.0.0
 * @since 2025/8/9 下午11:34
 */

@Repository
public class ResourceRepositoryImpl implements IResourceRepository {

    @Resource
    private IResourceMapper resourceMapper;

    @Override
    public List<ResourceEntity> findResourceByUserId(Long userId) {
        List<ResourceBO> resourceBOList = resourceMapper.findResourceByUserId(userId);
        return BeanUtil.copyToList(resourceBOList, ResourceEntity.class);
    }

    @Override
    public List<ResourceEntity> getMenuResourceList() {
        Wrapper<ResourceBO> wrapper = Wrappers.lambdaQuery(ResourceBO.class)
                .in(ResourceBO::getResourceType, ResourceConstants.SYSTEM, ResourceConstants.MENU, ResourceConstants.CATALOGUE)
                .orderByAsc(List.of(ResourceBO::getSortNo, ResourceBO::getId));
        List<ResourceBO> resourceBOList = resourceMapper.selectList(wrapper);
        return BeanUtil.copyToList(resourceBOList, ResourceEntity.class);
    }

    @Override
    public List<ResourceEntity> findResourceByRoleIds(List<Long> roleIdList) {
        List<ResourceBO> resourceBOList = resourceMapper.findResourceByRoleIds(roleIdList);
        return BeanUtil.copyToList(resourceBOList, ResourceEntity.class);
    }

    @Override
    public List<ResourceEntity> findSubResourceList(String parentResourceNo) {
        if (StringUtils.isBlank(parentResourceNo)) return List.of();
        Wrapper<ResourceBO> wrapper = Wrappers.lambdaQuery(ResourceBO.class)
                .likeRight(ResourceBO::getParentResourceNo, IdentifierNoUtil.processNo(parentResourceNo))
                .orderByAsc(ResourceBO::getSortNo);
        List<ResourceBO> resourceBOList = resourceMapper.selectList(wrapper);
        return BeanUtil.copyToList(resourceBOList, ResourceEntity.class);
    }

    @Override
    public IPage<ResourceEntity> findResourcePage(ResourceEntity resourceEntity, Integer pageNum, Integer pageSize) {
        Wrapper<ResourceBO> wrapper = getWrapper(resourceEntity);
        Page<ResourceBO> page = new Page<>(pageNum, pageSize);
        return resourceMapper.selectPage(page, wrapper)
                .convert(item -> BeanUtil.copyProperties(item, ResourceEntity.class));
    }

    @Override
    public boolean createResource(ResourceEntity resourceEntity) {
        ResourceBO resourceBO = BeanUtil.toBean(resourceEntity, ResourceBO.class);
        boolean result = resourceMapper.insert(resourceBO) > 0;
        if (!result) return false;
        BeanUtil.copyProperties(resourceBO, resourceEntity);
        return true;
    }

    @Override
    public Boolean updateResource(ResourceEntity resourceEntity) {
        ResourceBO resourceBO = BeanUtil.toBean(resourceEntity, ResourceBO.class);
        return resourceMapper.updateById(resourceBO) > 0;
    }

    @Override
    public List<ResourceEntity> listResource(ResourceEntity resourceEntity) {
        Wrapper<ResourceBO> wrapper = getWrapper(resourceEntity);
        List<ResourceBO> resourceBOList = resourceMapper.selectList(wrapper);
        return BeanUtil.copyToList(resourceBOList, ResourceEntity.class);
    }

    @Override
    public List<ResourceEntity> findSubLevel1ResourceList(String parentResourceNo) {
        Wrapper<ResourceBO> wrapper = Wrappers.lambdaQuery(ResourceBO.class)
                .eq(ResourceBO::getParentResourceNo, parentResourceNo);
        List<ResourceBO> resourceBOList = resourceMapper.selectList(wrapper);
        return BeanUtil.copyToList(resourceBOList, ResourceEntity.class);
    }

    private Wrapper<ResourceBO> getWrapper(ResourceEntity resourceEntity) {
        return Wrappers.lambdaQuery(ResourceBO.class)
                .eq(StringUtils.isNotBlank(resourceEntity.getResourceNo()), ResourceBO::getResourceNo, resourceEntity.getResourceNo())
                .eq(StringUtils.isNotBlank(resourceEntity.getResourceType()), ResourceBO::getResourceType, resourceEntity.getResourceType())
                .eq(resourceEntity.getDataState() != null, ResourceBO::getDataState, resourceEntity.getDataState())
                .likeRight(StringUtils.isNotBlank(resourceEntity.getParentResourceNo()),
                        ResourceBO::getParentResourceNo, StringUtils.defaultIfBlank(resourceEntity.getParentResourceNo(), IdentifierNoUtil.ROOT_PARENT_ID))
                .likeRight(StringUtils.isNotBlank(resourceEntity.getResourceName()), ResourceBO::getResourceName, resourceEntity.getResourceName())
                .likeRight(StringUtils.isNotBlank(resourceEntity.getRequestPath()), ResourceBO::getRequestPath, resourceEntity.getRequestPath())
                .likeRight(StringUtils.isNotBlank(resourceEntity.getLabel()), ResourceBO::getLabel, resourceEntity.getLabel())
                .orderByAsc(ResourceBO::getSortNo);
    }
}
